Module modules.shared
File name: shared.py Author: Martin Jůda Python Version: 3.7 Description: Shared code for more frames
Expand source code
"""
File name: shared.py
Author: Martin Jůda
Python Version: 3.7
Description: Shared code for more frames
"""
from abc import ABC, abstractmethod
from threading import Thread
import requests
from modules.config import VERIFY_SSL
from modules.constants import (
BACKEND_AUTH,
Errors,
)
class AsyncBackendCommunicator(Thread):
"""Class that handle communication with backend"""
def __init__(self, url, params=None, **kwargs):
super(AsyncBackendCommunicator, self).__init__(**kwargs)
self.url = url
self.result = None
self.is_result_ok = False
self.error_code = None
self.error_type = None
self.params = params
def run(self):
"""Async backend download function"""
try:
response = requests.get(
url=self.url,
auth=BACKEND_AUTH,
params=self.params,
timeout=(10, 60),
verify=VERIFY_SSL,
)
if response.status_code == 404:
self.error_code = 404
return
else:
response.raise_for_status()
except requests.HTTPError as err:
self.error_type = Errors.HTTP_REQUEST_ERROR
self.error_code = err.response.status_code
except requests.RequestException:
self.error_type = Errors.BACKEND_CONNECTION_ERROR
else:
self.result = response.json()
self.is_result_ok = True
class ActionInterface(ABC):
"""Interface for GUI frames"""
@abstractmethod
def post_init_actions(self):
"""Actions that starts after frame instantiation"""
pass
class ReadCardError(Exception):
"""Read card error"""
Classes
-
Interface for GUI frames
Expand source code
class ActionInterface(ABC): """Interface for GUI frames""" @abstractmethod def post_init_actions(self): """Actions that starts after frame instantiation""" passAncestors
- abc.ABC
Subclasses
Methods
-
Actions that starts after frame instantiation
Expand source code
@abstractmethod def post_init_actions(self): """Actions that starts after frame instantiation""" pass
-
Class that handle communication with backend
This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.
args is the argument tuple for the target invocation. Defaults to ().
kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.init()) before doing anything else to the thread.
Expand source code
class AsyncBackendCommunicator(Thread): """Class that handle communication with backend""" def __init__(self, url, params=None, **kwargs): super(AsyncBackendCommunicator, self).__init__(**kwargs) self.url = url self.result = None self.is_result_ok = False self.error_code = None self.error_type = None self.params = params def run(self): """Async backend download function""" try: response = requests.get( url=self.url, auth=BACKEND_AUTH, params=self.params, timeout=(10, 60), verify=VERIFY_SSL, ) if response.status_code == 404: self.error_code = 404 return else: response.raise_for_status() except requests.HTTPError as err: self.error_type = Errors.HTTP_REQUEST_ERROR self.error_code = err.response.status_code except requests.RequestException: self.error_type = Errors.BACKEND_CONNECTION_ERROR else: self.result = response.json() self.is_result_ok = TrueAncestors
- threading.Thread
Methods
-
Async backend download function
Expand source code
def run(self): """Async backend download function""" try: response = requests.get( url=self.url, auth=BACKEND_AUTH, params=self.params, timeout=(10, 60), verify=VERIFY_SSL, ) if response.status_code == 404: self.error_code = 404 return else: response.raise_for_status() except requests.HTTPError as err: self.error_type = Errors.HTTP_REQUEST_ERROR self.error_code = err.response.status_code except requests.RequestException: self.error_type = Errors.BACKEND_CONNECTION_ERROR else: self.result = response.json() self.is_result_ok = True
-
Read card error
Expand source code
class ReadCardError(Exception): """Read card error"""Ancestors
- builtins.Exception
- builtins.BaseException